package com.yunji.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @Project: yunji-coffee
 * @Package: com.yunji.common.utils
 * @Title: DecimalUtils
 * @Description: 小数工具类
 * <p>
 * History:
 * Date                     Version     Author          Summary
 * ============================================================
 * 2024-04-11 20:49:07      V1.0        HuaAo       新建类
 */

public class DecimalUtils {

    private static final Logger logs = LoggerFactory.getLogger(DecimalUtils.class);

    public static BigDecimal nullToZero(BigDecimal value) {
        return null == value ? BigDecimal.ZERO : value;
    }

    public static Integer toInt(Integer value) {
        return null == value ? 0 : value;
    }

    /**
     * 保留两位小数
     */
    public static BigDecimal leftTwo(BigDecimal value) {
        if (null == value) {
            return BigDecimal.ZERO;
        }
        return value.setScale(2, RoundingMode.HALF_UP);
    }

    /**
     * 不四舍五入 并不保留小数
     */
    public static BigDecimal getInteger(BigDecimal value) {
        if (null == value) {
            return BigDecimal.ZERO;
        }
        return value.setScale(0, RoundingMode.DOWN);
    }

    /**
     * @return boolean
     * @Title more
     * @Desc 大于0
     * @Date 2024-04-11 20:55:22
     * @Param value
     */
    public static boolean more(BigDecimal value) {
        if (value == null) return false;
        return value.compareTo(BigDecimal.ZERO) > 0;
    }

    /**
     * @return boolean
     * @Title less
     * @Desc 小于0
     * @Date 2024-04-11 20:57:41
     * @Param value
     */
    public static boolean less(BigDecimal value) {
        if (value == null) return false;
        return value.compareTo(BigDecimal.ZERO) < 0;
    }

    /**
     * @return boolean
     * @Title more
     * @Desc 大于等于0
     * @Date 2024-04-11 20:55:22
     * @Param value
     */
    public static boolean moreOrEquals(BigDecimal value) {
        if (value == null) return false;
        return value.compareTo(BigDecimal.ZERO) >= 0;
    }

    /**
     * @return boolean
     * @Title less
     * @Desc 小于等于0
     * @Date 2024-04-11 20:57:41
     * @Param value
     */
    public static boolean lessOrEqualsZero(BigDecimal value) {
        if (value == null) return true;
        return value.compareTo(BigDecimal.ZERO) <= 0;
    }

    /**
     * @return boolean
     * @Title more
     * @Desc v1 > v2
     * @Date 2024-04-11 20:56:46
     * @Param v1
     * @Param v2
     */
    public static boolean more(BigDecimal v1, BigDecimal v2) {
        if (v1 == null || v2 == null) return false;
        return v1.compareTo(v2) > 0;
    }

    /**
     * @return boolean
     * @Title less
     * @Desc v1 < v2
     * @Date 2024-04-11 20:56:55
     * @Param v1
     * @Param v2
     */
    public static boolean less(BigDecimal v1, BigDecimal v2) {
        if (v1 == null || v2 == null) return false;
        return v1.compareTo(v2) < 0;
    }

    /**
     * @return boolean true 等于0 不等于0
     * @Title equals
     * @Desc 判断与0相等
     * @Date 2024-04-11 20:51:40
     * @Param value 小数信息
     */
    public static boolean compare(BigDecimal value) {
        return BigDecimal.ZERO.compareTo(value) == 0;
    }

    /**
     * @return boolean
     * @Title equals
     * @Desc v1 = v2
     * @Date 2024-04-11 20:57:27
     * @Param v1
     * @Param v2
     */
    public static boolean compare(BigDecimal v1, BigDecimal v2) {
        if (v1 == null || v2 == null) return false;
        return v1.compareTo(v2) == 0;
    }

    /**
     * 提供精确的乘法运算。
     *
     * @param v1 被乘数
     * @param v2 乘数
     * @return 两个参数的积
     */
    public static Long multiply(BigDecimal v1, BigDecimal v2) {
        return v1.multiply(v2).longValue();
    }

    public static BigDecimal getOne() {
        return new BigDecimal("1");
    }


    public static void main(String[] args) {
        BigDecimal db = new BigDecimal("1.999").multiply(new BigDecimal("100"));
        logs.info("1.999 * 100 = {}", db);
        System.out.println(DecimalUtils.less(new BigDecimal("-0.001")));
    }
}
